const pkg = require('./package')
const devConfig = require('./config.js')
/**
 * 创建eslint
 */
const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: !devConfig.dev.showEslintErrorsInOverlay
  },
  exclude: /(node_modules)/
})
const cache = require('lru-cache')
const cheerio = require('cheerio')
module.exports = {
  mode: 'universal',

  /*
  ** Headers of the page
  */
  head: {
    title: '容猫',
    meta: [
      { charset: 'UTF-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=0' },
      { name: 'HandheldFriendly', content: 'true' },
      { hid: 'description', name: 'description', content: pkg.description },
      { 'http-equiv': 'X-UA-Compatible', content: 'ie=edge' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/rongcat.ico' },
      { href: '/css/base.css' },
      { ref: 'prefetch', href: '//static.rongcat.com/' },
      { ref: 'preload', href: '//static.rongcat.com/' },
      { ref: 'preconnect', href: '//static.rongcat.com/' },
      { ref: 'prefetch', href: '//img.rongcat.com' },
      { ref: 'preload', href: '//img.rongcat.com' },
      { ref: 'preconnect', href: '//img.rongcat.com' },
      { ref: 'prefetch', href: '//img.rongcat.net' },
      { ref: 'preload', href: '//img.rongcat.net' },
      { ref: 'preconnect', href: '//img.rongcat.net' }
    ],
    script: [
      { src: 'https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver', body: true }
      // { src: 'https://s4.cnzz.com/z_stat.php?id=1279234693&web_id=1279234693' }
    ]
  },

  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#fff' },

  /*
  ** Global CSS
  */
  css: [
    // 'assets/css/page-transletion.css',
    { src: 'swiper/dist/css/swiper.css', ssr: false }
  ],

  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
    { src: '~/plugins/vue-swiper.js', ssr: false },
    { src: '~/plugins/waterfall.js', ssr: false },
    { src: '~/plugins/baiduGa.js' },
    { src: '~/plugins/cache.js' },
    { src: '~/plugins/route.js' },
    { src: '~/plugins/directive.js', ssr: false },
    { src: '~/plugins/error.js', srr: false }
  ],

  /*
  ** Nuxt.js modules
  */
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/proxy',
    '@nuxtjs/toast'
  ],
  toast: { // toast模块的配置
    position: 'top-center',
    duration: 2000
  },
  axios: {
    prefix: 'https://api.rongcat.com'
  },
  proxy: {
    '/api': {
      target: 'https://log.rongcat.com', // 目标接口域名
      changeOrigin: true, // 表示是否跨域
      pathRewrite: {
        '^/api': '/' // 把 /api 替换成 /
      }
    }
  },

  /*
  ** Build configuration
  */
  build: {
    /*
    ** You can extend webpack config here
    */
    publicPath: '//static.rongcat.com/dist/ym-pc/',
    extractCSS: {
      allChunks: true,
      ignoreOrder: true
    },
    // 查看打包后的大小
    // analyze: true,
    // postcss: {
    // // 添加插件名称作为键，参数作为值
    // // 使用npm或yarn安装它们
    //   // plugins: {
    //   // // 通过传递 false 来禁用插件
    //   //   'postcss-url': true,
    //   //   'postcss-nested': {},
    //   //   'postcss-responsive-type': {},
    //   //   'postcss-hexrgba': {}
    //   // },

    //   preset: {
    //   // 更改postcss-preset-env 设置
    //     autoprefixer: {
    //       grid: true
    //     }
    //   }
    // },
    vendor: ['axios', 'js-cookie'],
    extend(config, ctx) {
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          ...(devConfig.dev.useEslint ? createLintingRule() : {})
        })
      }
    }
  },
  render: {
    resourceHints: false,
    bundleRenderer: {
      // eslint-disable-next-line new-cap
      cache: new cache({
        max: 1000,
        maxAge: 1000 * 60 * 15
      })
    }
  },
  router: {
    middleware: 'auth',
    prefetchLinks: false
  },
  hooks: {
    'render:route': (url, result) => {
      this.$ = cheerio.load(result.html, { decodeEntities: false })
      // 由于window.__nuxt__总是位于body中的第一个script中，
      // 所以我移除了body中第一个脚本标签
      this.$(`body script`).eq(0).remove()
      result.html = this.$.html()
    }
  }
}
